package com.fingard.dsp.bank.directbank.gdb01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class GDB01Deposit extends GDB01Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            GDB01Header bankHeader = new GDB01Header();
            bankHeader.tranCode = "0054";
            bankHeader.retCode="000";//固定值，比填项
            bankHeader.entSeqNo = getQryTransID();
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("account", p_req7507.actNum);
            xmlWriter.writeElementString("reserve1", "");
            xmlWriter.writeElementString("reserve2", "");
            xmlWriter.writeElementString("reserve3", "");
            xmlWriter.writeElementString("reserve4", "");
            xmlWriter.writeElementString("reserve5", "");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            //模拟返回
            //WriteBankLogStep2(xmlWriter.toXmlString());
            //String[] tmpStrRet=new String[2];
            //tmpStrRet[0]="";
            //tmpStrRet[1]=FileHelper.readFileText("D:\\zjbr-开发便捷小工具类\\xmlDealWith\\cgn02Test.txt", "utf-8");
            //WriteBankLogStep3(tmpStrRet[1]);

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
                    if ("000".equals(retCode)) {//返回200表示查询成功，可以继续后续数据处理
                        String totalCount= tmpBkReader.getFirstText("/BEDC/Message/Body/totalCount");
                        int num=0;
                        while (tmpBkReader.read("/BEDC/Message/Body/records/record")){
                            XElement readXElement=tmpBkReader.readedXElement;
                            RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                            retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                            retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                            retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("openNodeName").trim();//定存单号
                            retAts7507Detail.startDate=readXElement.getSingleTextNotNull("accountOpenDate").trim();//起始日期
                            retAts7507Detail.endDate=readXElement.getSingleTextNotNull("closeDate").trim();//截止日期
                            retAts7507Detail.valueDate=readXElement.getSingleTextNotNull("accrualStartDate").trim();//起息日期
                            String terms=readXElement.getSingleTextNotNull("periodCode").trim();//期限
                            retAts7507Detail.terms=getBankConfig().getMapValueByKey("termMapping",terms);//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                            retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("productType"));//币种
                            retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("baseAmount").trim();//金额
                            retAts7507Detail.rate=readXElement.getSingleTextNotNull("rate").trim().replace("%","");//利率(%)
                            retAts7507Detail.interestMoney=readXElement.getSingleTextNotNull("reserve1").trim();//利息
                            String depositStates=readXElement.getSingleTextNotNull("mediumState").trim();//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                            if(StringHelper.hasAnyChar(depositStates)){
                                switch (Integer.parseInt(depositStates)){
                                    case 1:depositStates="2";break;
                                    case 2:depositStates="1";break;
                                    case 3:depositStates="7";break;
                                    case 4:depositStates="9";break;
                                    case 5:depositStates="9";break;
                                    case 6:depositStates="9";break;
                                    default:depositStates="";//7-挂失补发;8-已换开;9-已质押; 系统没有此状态，默认返回空
                                }
                            }
                            retAts7507Detail.depositStates=depositStates;
                            String dumpFlag=readXElement.getSingleTextNotNull("autoSaveFlag").trim();//到期处理方式 U-不处理;B-本利自动续存;P-本金自动续存;A-本利自动转活期
                            if(StringHelper.hasAnyChar(dumpFlag)){
                                if("U".equals(dumpFlag)){
                                    dumpFlag="1";
                                }else if("B".equals(dumpFlag)){
                                    dumpFlag="2";
                                }else {
                                    dumpFlag="";
                                }
                            }
                            retAts7507Detail.dumpFlag=dumpFlag;
                            retAts7507Detail.drawDate="";//上划日期
                            retAts.writeRetDetail(retAts7507Detail);
                            num++;
                        }
                        if(num!=Integer.parseInt(totalCount)){
                            tmpRespCode = "-1";
                            tmpRespInfo = "返回总笔数与遍历笔数不一致";
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
